﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace 售后客户管理.Forms
{
    public partial class FmWorkPlanList : Form
    {

        public FmWorkPlanList()
        {
            InitializeComponent();
            this.toolTipCreateNew.SetToolTip(this.btnCreateNew, "新建");
            this.toolTipEdit.SetToolTip(this.btnEdit, "编辑");
            this.toolTipRefresh.SetToolTip(this.btnRefresh, "查询");
            this.dataGridView1.DoubleBuffered(true);
        }
        private Common.SqlConnectionHelper SqlConnectionHelper = new Common.SqlConnectionHelper(Program.configs, ConfigCode.Main);

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCreateNew_Click(object sender, EventArgs e)
        {
            ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan();
            workPlan.userName = this.cbbUser.Text;
            FmWorkPlan fmWorkPlan = new FmWorkPlan(workPlan, SqlConnectionHelper.sqlConnection);
            if (fmWorkPlan.ShowDialog() == DialogResult.OK)
            {
                workPlan = new ModelLibrary.WorkPlan() { userName = this.cbbUser.Text };
                this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
                DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                DataGridViewRow dgvRow = this.dataGridView1.SelectedRows[0];
                ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan() { planID = dgvRow.Cells["planID"].Value.ToString() };
                System.Data.DataTable dataTable = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
                workPlan = DrawingImage.DataTableToObject<ModelLibrary.WorkPlan>(dataTable, workPlan);

                FmWorkPlan fmWorkPlan = new FmWorkPlan(workPlan, SqlConnectionHelper.sqlConnection);
                if (fmWorkPlan.ShowDialog() == DialogResult.OK)
                {
                    workPlan = new ModelLibrary.WorkPlan() { userName = this.cbbUser.Text };
                    this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
                    DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
                }
            }
        }



        private void FmWorkPlanList_Load(object sender, EventArgs e)
        {
            if (SqlConnectionHelper.testLinks())
            {
                this.cbbUser.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("User", SqlConnectionHelper.sqlConnection);
                ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan();
                workPlan.userName = this.cbbUser.Text;
                this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
                DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
            }
            this.dataGridView1.BackgroundColor = Common.MyColor.NewColor_Lavender;

        }

        private void cbbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan();
            workPlan.userName = this.cbbUser.Text;
            this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
        }

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            DataGridViewRow dgvRow = this.dataGridView1.SelectedRows[0];
            ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan() { planID = dgvRow.Cells["planID"].Value.ToString() };
            System.Data.DataTable dataTable = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
            workPlan = DrawingImage.DataTableToObject<ModelLibrary.WorkPlan>(dataTable, workPlan);

            FmWorkPlan fmWorkPlan = new FmWorkPlan(workPlan, SqlConnectionHelper.sqlConnection);
            if (fmWorkPlan.ShowDialog() == DialogResult.OK)
            {
                workPlan = new ModelLibrary.WorkPlan() { userName = this.cbbUser.Text };
                this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
            }
        }

        private void 导出EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = "";
            SaveFileDialog saveFile = new SaveFileDialog
            {
                Filter = "2010Excel文件(*.xlsx)|*.xlsx",
                FilterIndex = 1,
                Title = "保存EXCEL为"
            };
            if (saveFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            filePath = saveFile.FileName;

            string sheetName = "Sheet1";
            DataTable dataTable = DrawingImage.DataGridViewToDataTable(this.dataGridView1, ModelLibrary.ColumnType.HeaderText, sheetName, true);
            List<string> sheetNameList = (from d in dataTable.AsEnumerable() select d.Field<string>("年月周")).Distinct().ToList();
            DataTable dt = dataTable.Clone();
            if (sheetNameList.Count > 0)
                for (int i = 0; i < sheetNameList.Count; i++)
                {
                    dt.Clear();
                    dt.TableName = sheetNameList[i];
                    DataRow[] dr = dataTable.Select("年月周='" + sheetNameList[i] + "'");
                    for (int j = 0; j < dr.Count(); j++)
                    {
                        DataRow dr_new = dt.NewRow();
                        dr_new.ItemArray = dr[j].ItemArray;
                        dt.Rows.Add(dr_new);
                    }


                    if (!Common.ExcelHelper.OutputExcelFile(dt, filePath))
                    {
                        MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            if (MessageBox.Show("保存成功！\n是否需要打开此文件？？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                System.Diagnostics.Process.Start(filePath);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ModelLibrary.WorkPlan workPlan = new ModelLibrary.WorkPlan();
            workPlan.userName = this.cbbUser.Text;
            this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetWorkPlanDT(workPlan, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
        }
    }
}
